# -*- coding:utf-8 -*-
"""
@author: fsksf

@since: 2022/4/9 9:42
"""
import enum
from dataclasses import dataclass
from typing import List
from vcat.core.event import EventType


class Dtype(enum.Enum):
    Table = 'Table'
    Form = 'Form'
    Chart = 'Chart'


class MetaObj:

    def meta(self):
        print('作为 MetaObj 必须实现 meta方法')
        raise NotImplementedError

    def to_dict(self):
        meta = self.meta()
        return {
            x['field']: getattr(self, x['field']) for x in meta['headers']
        }


@dataclass
class TagMetaInfo:
    """
    headers信息、输入框input信息
    """
    name: str                   # 用户可见显示
    field: str                  # 后台标识
    value: str = ''             # 值, 默认为空字符串
    display: bool = True        # 本条是否可见
    editable: bool = False      # 本条是否可编辑

    def to_json(self):
        return {
            'name': self.name,
            'field': self.field,
            'display': self.display,
            'editable': self.editable,
            'value': self.value
        }


@dataclass
class DivMetaInfo:

    name: str           # 英文唯一名称
    title: str          # 中文展示名称
    headers: List[TagMetaInfo]
    index: int = 100    # 页面中的排序
    style: str = 'height:150px;width:100%'
    dtype: str = Dtype.Table.name
    event: str = EventType.LOG.name

    def to_dict(self):
        return {
            'name': self.name,
            'style': self.style,
            'title': self.title,
            'dtype': self.dtype,
            'index': self.index,
            'event': self.event,
            'headers': [
                row.to_json() for row in self.headers
            ]
        }


@dataclass
class Children(MetaObj):
    name: str
    path: str
    component: str
    props: dict
    meta: dict

    def to_dict(self):
        return {
            'name': self.name,
            'component': self.component,
            'path': self.path,
            'props': self.props,
            'meta': self.meta
        }


@dataclass
class MainRoutes(MetaObj):
    name: str
    path: str
    children: List[Children]
    redirect: str
    meta: dict
    component: str = 'Layout'

    def to_dict(self):
        json = {
            'path': self.path,
            'children': [ch.to_dict() for ch in self.children],
            'component': self.component,
            'meta': self.meta
        }
        if self.redirect:
            json['redirect'] = self.redirect

        return json
